package com.photoselector.ui;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;

import com.photoselector.model.ImageBean;
import com.photoselector.ui.PhotoItem.onItemClickListener;
import com.photoselector.ui.PhotoItem.onMaxListener;
import com.photoselector.ui.PhotoItem.onPhotoItemCheckedListener;
import com.ykdl.app.R;


/**
 * 
 * @author Aizaz AZ
 *
 */


public class PhotoSelectorAdapter extends MBaseAdapter<ImageBean> {

	private int itemWidth;
	private int horizentalNum = 3;
	private onPhotoItemCheckedListener listener;
	private LayoutParams itemLayoutParams;
	private onItemClickListener mCallback;
	private OnClickListener cameraListener;
	private int mCheckedNumber = 0;
	private int MAX_IMAGE = 0;
	private onMaxListener mOnMaxListener;
	
	private ArrayList<ImageBean>  mCheckedList;
	private PhotoSelectorAdapter(Context context, ArrayList<ImageBean> models) {
		super(context, models);
	}

	public PhotoSelectorAdapter(Context context, ArrayList<ImageBean> models, int screenWidth, onPhotoItemCheckedListener listener, onItemClickListener mCallback,
			OnClickListener cameraListener,onMaxListener mOnMaxListener) {
		this(context, models);
		setItemWidth(screenWidth);
		this.listener = listener;
		this.mCallback = mCallback;
		this.mOnMaxListener = mOnMaxListener;
		this.cameraListener = cameraListener;
		this.MAX_IMAGE = 2;
		
	}


	public void setItemWidth(int screenWidth) {
		int horizentalSpace = context.getResources().getDimensionPixelSize(R.dimen.sticky_item_horizontalSpacing);
		this.itemWidth = (screenWidth - (horizentalSpace * (horizentalNum - 1))) / horizentalNum;
		this.itemLayoutParams = new LayoutParams(itemWidth, itemWidth);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PhotoItem item = null;
		if (convertView == null || !(convertView instanceof PhotoItem)) {
			item = new PhotoItem(context, listener, mOnMaxListener);
			item.setLayoutParams(itemLayoutParams);
			item.setAdapter(this);
			convertView = item;
		} else {
			item = (PhotoItem) convertView;
		}
		ImageBean ImageBean = models.get(position);
		String mTruePath = 	ImageBean.getOriginalPath();
		
		boolean isChecked = 	models.get(position).isChecked();
		
		item.setImageDrawable(models.get(position));
		item.setGetView(isChecked);
		item.setSelected(isChecked);
		item.setOnClickListener(mCallback, position);
		setCheckedPhoto(mTruePath, item,ImageBean);
		return convertView;
	}
	public int getmCheckedNumber() {
		return mCheckedNumber;
	}

	public void setmCheckedNumber(int mCheckedNumber) {
		this.mCheckedNumber = mCheckedNumber;
	}
	
	public int getMAX_IMAGE() {
		return MAX_IMAGE;
	}
	public void setmCheckedList(ArrayList<ImageBean>  mCheckedList) {
		this.mCheckedList = mCheckedList;
	}
	private void setCheckedPhoto(String mTruePath,PhotoItem item,ImageBean ImageBean){
		
		try {
			if (mCheckedList != null && mCheckedList.size() > 0) {
				mCheckedNumber = mCheckedList.size();

				for (ImageBean i : mCheckedList) {
					
						ImageBean info = (ImageBean) i;
						if (info.getOriginalPath().equals(mTruePath)) {
							item.setSelected(true);
							ImageBean.setChecked(true);
						}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
